<template>
  <!-- PAGE CONTENT BEGINS -->
  <div>
    <p>
      &nbsp;
      <button v-on:click="list(1)" class="btn btn-white btn-warning btn-round">
        <i class="ace-icon fa fa-refresh bigger-120 blue"></i>
        刷新
      </button>
    </p>

    <div class="row">
      <div class="col-md-6">
        <textarea id="resource-textarea" class="form-control" v-model="resourceStr" rows="10"></textarea>
        <br>
        <button v-on:click="save()" id="save-btn" type="button" class="btn btn-white btn-warning btn-round">
          <i class="ace-icon fa fa-save bigger-120 blue"></i>
          保存
        </button>
      </div>
      <div class="col-md-6">
        <ul id="tree" class="ztree"></ul>
      </div>
    </div>
    <br>
  </div>
</template>
<script>
export default {
  name: 'system-resource',
  data: function (){
    return{
      //映射表单数据
      resource: {},
      resources: [],
      resourceStr:"",
      tree:{},
    }
  },
  mounted:function (){
    let _this = this;
    _this.list();
  },
  methods:{
    /**查询tree*/
    list(){
      let _this = this;
      Loading.show();
      _this.$ajax.get(process.env.VUE_APP_SERVER + '/system/admin/resource/load-tree').then((response)=>{
        Loading.hide();
        let resp = response.data;
        _this.resources = resp.content;
        //初始化树
        _this.initTree();
      })
    },
    /**保存*/
    save(){
      let _this = this;
      /**保存校验*/
      if(Tool.isEmpty(_this.resourceStr)){
        toast.warning("资源不能为空！");
        return;
      }
      let json = JSON.parse(_this.resourceStr);
      Loading.show();
      _this.$ajax.post(process.env.VUE_APP_SERVER + '/system/admin/resource/save',json).then((response)=>{
        Loading.hide();
        console.log("保存资源列表结果：",response);
        let resp = response.data;
        if(resp.success){
          $("#myFormModal").modal("hide");
          _this.list(1);
          toast.success("保存成功！");
        }else{
          toast.warning(resp.message);
        }


      })
    },
    /**删除*/
    del(id){
      let _this = this;
      //弹出框
      Confirm.show("删除资源后不可恢复，您是否检查删除!",function (){
        _this.$ajax.delete(process.env.VUE_APP_SERVER + '/system/admin/resource/delete/'+ id )
            .then((response)=>{
              console.log("删除资源列表结果：",response);
              let resp = response.data;
              if(resp.success){
                _this.list(1);
                toast.success("删除成功！");
              }
            });
      })
    },

    /**
     * 初始化资源树
     */
    initTree(){
      let _this =this;
      let setting = {
        data:{
          simpleData:{
            idKey:"id",
            pIdKey:"parent",
            rootPId:"",
          }
        }
      };

      _this.zTree = $.fn.zTree.init($("#tree"),setting,_this.resources)
      _this.zTree.expandAll(true);
    }
  }
}
</script>